package com.feng.newconfig;

import io.github.resilience4j.timelimiter.TimeLimiterConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.circuitbreaker.resilience4j.Resilience4JCircuitBreakerFactory;
import org.springframework.cloud.circuitbreaker.resilience4j.Resilience4JConfigBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.time.Duration;

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class Resilience4jClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(Resilience4jClientApplication.class, args);
    }

    /**
     * 修改断路器默认配置
     *
     * @return
     */
//    @Bean
//    public Customizer<Resilience4JCircuitBreakerFactory> slowCustomizer() {
//        return factory -> factory.configure(builder -> builder
//                .timeLimiterConfig(TimeLimiterConfig.custom()
//                        .timeoutDuration(Duration.ofSeconds(5)).build())
//                .circuitBreakerConfig(
//                        CircuitBreakerConfig.ofDefaults()), "config-03");
//    }

    @Resource
    Resilience4JCircuitBreakerFactory resilience4JCircuitBreakerFactory;

    /**
     * 修改断路器默认配置
     *
     * @return
     */
    @PostConstruct
    public void defaultCustomizer() {
        //默认超时规则,默认1s,不使用断路器超时规则可以设置大一点
        resilience4JCircuitBreakerFactory.configureDefault(id -> new Resilience4JConfigBuilder(id)
                .timeLimiterConfig(TimeLimiterConfig.custom().timeoutDuration(Duration.ofMillis(2000)).build()).build());
    }
}
